package com.moli.iov.open.authentication;

import com.moli.iov.open.model.vo.req.ClientDetailsVo;
import com.moli.iov.rest.GenericCode;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * token
 * </p>
 *
 * @author zql
 * @since 2019/9/23 13:21
 */
@Slf4j
@RestController
public class TokenController {

  @Autowired
  private TokenServices tokenServices;

  /**
   * 获取access_token
   * @param appId
   * @param secret
   * @return
   */
  @GetMapping("/token")
  public RestResponse token(@RequestParam(name = "grant_type", required = false) String grantType,
      @RequestParam(name = "appid", required = false) String appId,
      @RequestParam(name = "secret", required = false) String secret){

    log.info("token入参 grantType: {}， appid:{}, secret:{}", grantType, appId, secret);

    if(StringUtil.isBlank(grantType) || StringUtil.isBlank(appId) || StringUtil.isBlank(secret)){
      return RestResponse.failure(GenericCode.OPEN_PARAM_ERROR);
    }

    return tokenServices.token(new ClientDetailsVo(appId, secret));
  }
}
